﻿using Furion.DependencyInjection;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace PrivateTracker.NET.Application
{
    public class PeerSevice : IPeerService, ITransient
    {
        private static ConcurrentDictionary<string, List<Peer>> _peers;
        public PeerSevice()
        {
            if (_peers == null)
            {
                _peers = new ConcurrentDictionary<string, List<Peer>>();
            }
        }
        /// <summary>
        /// 添加做种Peer
        /// </summary>
        /// <param name="announceInput"></param>
        /// <returns></returns>
        public async Task<IList<Peer>> AddPeerAsync(AnnounceInput announceInput)
        {
            var peer = new Peer
            {
                PeerId = announceInput.Peer_Id,
                DownLoaded = announceInput.Downloaded,
                Uploaded = announceInput.Uploaded,
                Ip = announceInput.Ip,
                Port = announceInput.Port
            };
            var exist = await IsExistByHashInfoAsync(announceInput.Info_Hash);
            if (!exist)//&& announceInput.Event == PeerEvent.Started && (announceInput.Left == 0 || announceInput.Uploaded > 0)
            {
                _peers.TryAdd(announceInput.Info_Hash, new List<Peer> { peer });
            }
            else if (exist && !_peers[announceInput.Info_Hash].Any(o => o.PeerId == announceInput.Peer_Id))
            {
                _peers[announceInput.Info_Hash].Add(peer);
            }
            return await Task.FromResult<List<Peer>>(_peers[announceInput.Info_Hash]);
        }

        /// <summary>
        /// 删除Peer
        /// </summary>
        /// <param name="announceInput"></param>
        /// <returns></returns>
        public async Task<int> DeletePeerAsync(AnnounceInput announceInput)
        {
            var b = await IsExistByHashInfoAsync(announceInput.Info_Hash);
            if (!b) return await Task.FromResult<int>(0);
            return await Task.FromResult(_peers[announceInput.Info_Hash].RemoveAll(p => p.PeerId == announceInput.Peer_Id));
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="infoHash"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<IList<Peer>> GetPeersByInfoHashAsync(string infoHash)
        {
            var exist = await IsExistByHashInfoAsync(infoHash);
            if (exist)
            {
                return await Task.FromResult<List<Peer>>(_peers[infoHash]);
            }
            else
            {
                return await Task.FromResult<List<Peer>>(null);
            }
        }

        /// <summary>
        /// 种子是否存在
        /// </summary>
        /// <param name="hashInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> IsExistByHashInfoAsync(string hashInfo)
        {
            return await Task.FromResult(_peers.ContainsKey(hashInfo));
        }
    }
}
